
const { DXDeck } = require("../game_model/dxDeck");
const { DeckMgr } = require("./deckMgr");
const { SeatMgr } = require("./seatMgr");
const { DealProcessMgr } = require("./dealProcessMgr");
const { DXBettingProcessMgr, BettingType } = require("./dxBettingProcessMgr");
const { RobBankerProcessMgr, RobBankerType } = require("./robBankerProcessMgr");
const { ShowCardProcessMgr } = require("./showCardProcessMgr");
const { DXSettleProcessMgr } = require("./dxSettleProcessMgr");
const { DXNetMgr } = require("./dxNetMgr");
const { RecordMgr } = require("./recordMgr");
const { DXCardMgr } = require("./dxCardMgr");
const { Signal, SIGNAL_DEFINE } = require("./signal");

const GAME_STATE = {
    BEGIN: 0,       // 开始阶段
    DEAL_CARD: 1,   // 发牌阶段
    ROB_BANKER: 2,  // 抢庄阶段
    BETTING: 3,     // 下注阶段
    SHOW_CARD: 4,   // 亮牌阶段
    SETTLE: 5,      // 结算阶段
    READY_NEXT: 6,  // 准备下一把游戏
};

class DXGameProcessMgr {
    constructor(roomInfo) {
        this.signal = new Signal();
        this.roomInfo = roomInfo;
        this.banker = null;
        this.dealCardTime = 2;
        this.robBankerTime = 2;
        this.bettingTime = 10;
        this.showCardTime = 4;
        this.nextGameTime = 5;
        

        var deck = new DXDeck();
        this.deckMgr = new DeckMgr();
        this.deckMgr.setDeck(deck);

        this.seatMgr = new SeatMgr();
        this.seatMgr.setRoomInfo(roomInfo);
        this.seatMgr.setSignal(this.signal);

        this.dealProcessMgr = new DealProcessMgr();
        this.dealProcessMgr.setNextAction(this.onActionFinish);
        this.dealProcessMgr.setSeatMgr(this.seatMgr);
        this.dealProcessMgr.setSignal(this.signal);
        this.dealProcessMgr.setDealCardTime(this.dealCardTime);
        this.dealProcessMgr.setDeckMgr(this.deckMgr);
        this.dealProcessMgr.setCardNum(2);

        this.robBankerProcessMgr = new RobBankerProcessMgr();
        this.robBankerProcessMgr.setNextAction(this.onActionFinish);
        this.robBankerProcessMgr.setSeatMgr(this.seatMgr);
        this.robBankerProcessMgr.setSignal(this.signal);
        this.robBankerProcessMgr.setRobBankerType(RobBankerType.RANDOM);
        this.robBankerProcessMgr.setDecideBankerTime(2);

        this.bettingProcessMgr = new DXBettingProcessMgr();
        this.bettingProcessMgr.setNextAction(this.onActionFinish);
        this.bettingProcessMgr.setSeatMgr(this.seatMgr);
        this.bettingProcessMgr.setGameMgr(this);
        this.bettingProcessMgr.setSignal(this.signal);

        this.showCardProcessMgr = new ShowCardProcessMgr();
        this.showCardProcessMgr.setNextAction(this.onActionFinish);
        this.showCardProcessMgr.setGameMgr(this);
        this.showCardProcessMgr.setSignal(this.signal);
        this.showCardProcessMgr.setCardMgr(new DXCardMgr());

        this.settleProcessMgr = new DXSettleProcessMgr();
        this.settleProcessMgr.setNextAction(this.onActionFinish);
        this.settleProcessMgr.setSeatMgr(this.seatMgr);
        this.settleProcessMgr.setSignal(this.signal);
        this.settleProcessMgr.setGameMgr(this);

        this.netMgr = new DXNetMgr();
        this.netMgr.setGameMgr(this);
        this.netMgr.setSeatMgr(this.seatMgr);
        this.netMgr.setSignal(this.signal);
        
        this.recordMgr = new RecordMgr();
        this.recordMgr.setGameMgr(this);
        this.recordMgr.setSeatMgr(this.seatMgr);
        this.recordMgr.setSignal(this.signal);
    }

    setRoomInfo(roomInfo) {
        this.roomInfo = roomInfo;
        this.seatMgr.setSeatNum(roomInfo.conf.roomNum);
        this.seatMgr.setRoomInfo(roomInfo);
        console.log("房间信息", roomInfo);
    }

    getRoomInfo() {
        return this.roomInfo;
    }

    getBanker() {
        this.robBankerProcessMgr.getBanker();
    }

    getBaseScore() {
        return this.roomInfo.conf.difen;
    }

    getFundPool() {
        return this.fundPool;
    }

    getCurrentRound() {
        return this.roomInfo.numOfGames;
    }

    getBanker() {
        return this.banker;
    }

    isInfiniteRound() { // 不夜场
        return this.maxGames < 0;
    }

    AddFundPool(fund) {
        this.fundPool += fund;
    }

    getGameType() {
        return "pk_dx";
    }

    getRoomId() {
        return this.roomInfo.roomId;
    }

    getCreateTime() {
        return this.roomInfo.createTime;
    }

    isRoomOver() {
        if (this.forceEnd) {
            return true;
        }
        if (!this.isInfiniteRound()) {
            return this.roomInfo.numOfGames >= this.roomInfo.maxGames;
        } else {
            return false;
        }
    }

    onActionFinish() {
        if (this.gameState == GAME_STATE.BEGIN) {
            console.log("目前处于发牌阶段");
            this.gameState = GAME_STATE.DEAL_CARD;
            this.dealProcessMgr.doAction();
        } else if (this.gameState == GAME_STATE.DEAL_CARD) {
            console.log("目前处于抢庄阶段");
            this.gameState = GAME_STATE.ROB_BANKER;
            this.robBankerProcessMgr.doAction();
        } else if (this.gameState == GAME_STATE.ROB_BANKER) {
            console.log("目前处于下注阶段");
            this.gameState = GAME_STATE.BETTING;
            this.banker = this.robBankerProcessMgr.getBanker();
            this.bettingProcessMgr.doAction();
        } else if (this.gameState == GAME_STATE.BETTING) {
            console.log("目前处于亮牌阶段");
            this.gameState = GAME_STATE.SHOW_CARD;
            this.showCardProcessMgr.doAction();
        } else if (this.gameState == GAME_STATE.SHOW_CARD) {
            console.log("目前处于结算阶段");
            this.gameState = GAME_STATE.SETTLE;
            this.settleProcessMgr.doAction();
        } else if (this.gameState == GAME_STATE.SETTLE) {
            console.log("目前处于结算完成阶段");
            if (isRoomOver()) {
                roomMgr.destroy(roomId);
            } else {
                beginGame();
            }
        }
    }

    initConf(isOnlyEat, isReverseBelt) {
        this.isOnlyEat = isOnlyEat;
        this.isReverseBelt = isReverseBelt;
        
        if (isReverseBelt) {
            this.bettingProcessMgr.setBettingType(BettingType.TAKE_TURNS_BANKER);
        } else {
            this.bettingProcessMgr.setBettingType(BettingType.TAKE_TURNS_ONE_ROUND);
        }
    }
    
    sitDown(userId, value) {
        this.seatMgr.sitDown(userId, value);
    }

    beginGame() {
        this.gameState = GAME_STATE.BEGIN;
        this.winner = null;

        if (this.fundPool == null) {
            this.fundPool = 0;
        }
        this.fundPool += this.getBaseScore() * this.seatMgr.getGamberNum();

        this.seatMgr.refreshOnlineUser();
        console.log("当前赌徒人数", this.seatMgr.getGamberNum());
        console.log(this.seatMgr.gambers);
        console.log(this.seatMgr.users);
        if (this.seatMgr.getGamberNum() <= 1) {
            this.forceEnd = true;
            this.settleProcessMgr.doAction();
            return;
        }

        this.roomInfo.gameState = "playing";
        this.roomInfo.numOfGames++;
        this.signal.emit(SIGNAL_DEFINE.GAME_BEGIN, this.getCurrentRound());

        this.onActionFinish();
    }

    
}

exports.DXGameProcessMgr = DXGameProcessMgr;